<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>矩阵运算</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }

        #canvas {
            background-color: antiquewhite;
        }
    </style>
</head>

<body>
<canvas id="canvas"></canvas>
<!-- 顶点着色器 -->
<script id="vertexShader" type="x-shader/x-vertex">
    void main(){
      //点位
      gl_Position=vec4(0,0,0,1);
      //尺寸
      gl_PointSize=512.0;
    }
  </script>
<!-- 片元着色器 -->
<script id="fragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    vec4 v=vec4(1,2,3,4)+vec4(5,6,7,8);
    void main(){
      /*
      mat4 m=mat4(
        1,5,9,13,
        2,6,10,14,
        3,7,11,15,
        4,8,12,16
      );
      m+=1.0;
      m-=1.0;
      m*=2.0;
      m/=2.0;
      */
      mat4 m=mat4(
        2,16,8,8,
        4,8,8,8,
        8,4,8,8,
        16,8,8,8
      );
      mat4 n=mat4(
        1,4,1,2,
        2,4,2,1,
        4,4,1,2,
        8,4,2,1
      );

      /*
      m+=n;
      m-=n;
      m/=n;
      */
      m*=n;



      float dist=distance(gl_PointCoord,vec2(0.5,0.5));
      if(dist>=0.0&&dist<0.125){
        gl_FragColor=m[0]/255.0;
      }else if(dist>=0.125&&dist<0.25){
        gl_FragColor=m[1]/255.0;
      }else if(dist>=0.25&&dist<0.375){
        gl_FragColor=m[2]/255.0;
      }else if(dist>=0.375&&dist<0.5){
        gl_FragColor=m[3]/255.0;
      }else{
        discard;
      }
    }
  </script>
<script type="module">
  import { initShaders } from "../jsm/Utils.js";

  const canvas = document.querySelector("#canvas");
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;

  // 获取着色器文本
  const vsSource = document.querySelector("#vertexShader").innerText;
  const fsSource = document.querySelector("#fragmentShader").innerText;

  //三维画笔
  const gl = canvas.getContext("webgl");

  //初始化着色器
  //功能：解析着色器文本，整合到程序对象里，关联webgl上下文对象，实现两种语言的相互通信
  initShaders(gl, vsSource, fsSource);

  //声明颜色 rgba
  gl.clearColor(0, 0, 0, 1);
  //刷底色
  gl.clear(gl.COLOR_BUFFER_BIT);

  //绘制顶点
  gl.drawArrays(gl.POINTS, 0, 1);

  //圆环宽度
  const vw = 512 / 8;
  //遍历圆环
  for (let i = 0; i < 4; i++) {
    logPixel(vw * i + vw / 2)
  }


  function logPixel(offset = 0) {
    //像素容器
    const pixel = new Uint8Array(4)
    //抓取像素
    gl.readPixels(
      canvas.width / 2 + offset,
      canvas.height / 2,
      1, 1,
      gl.RGBA,
      gl.UNSIGNED_BYTE,
      pixel
    )

    console.log(pixel);
  }
</script>
</body>

</html>
